package org.pluslibs.framework.swingplus.jdialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;

import javax.swing.JLabel;

import org.pluslibs.framework.swingplus.utils.MediaCache;
import org.pluslibs.framework.swingplus.utils.SwingUtils;

public class JDialogError extends JDialogMessage {

    private static final long serialVersionUID = 6267310677193069435L;
    private static final int MESSAGE_WIDTH_BEFORE_WRAP = 140;

    public static void main(String args[]) {
        SwingUtils.ensureLookAndFeel();
        new JDialogError((java.awt.Frame) null, new Exception("testing"));
    }

    public JDialogError(Window parent, Throwable e) {
        super(parent, true);
        initComponents();
        txtMessage.setFont(lblHeading.getFont());
        if (e != null) {
            setTitle(e.getClass().getName());
            String message = e.getMessage();
            if (message != null) {
                if (message.length() >= MESSAGE_WIDTH_BEFORE_WRAP) {
                    // Set to multiline
                    /*
                     * txtMessage.setLineWrap(true); txtMessage.setWrapStyleWord(true); txtMessage.setRows(3);
                     */

                    // Make wider than normal (or else will be minimum width)
                    Dimension preferedsize = lblHeading.getPreferredSize();
                    lblHeading.setPreferredSize(new Dimension(450, preferedsize.height));
                } else {
                    /*
                     * txtMessage.setLineWrap(false); txtMessage.setWrapStyleWord(false); txtMessage.setRows(1);
                     */
                }
                txtMessage.setText(message);
            } else {
                txtMessage.setText("Exception gave no description");
            }
        } else {
            setTitle("Exception was null");
            txtMessage.setText("An exception was thrown, but the exception was null");
        }
        pack();
        SwingUtils.centerParent(this);
        super.setVisible(true);
    }

    private void initComponents() {
        jPanel5 = new javax.swing.JPanel();
        lblHeading = new javax.swing.JLabel();
        lblPicture = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        scrollMessage = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(15, 20, 10, 20));
        lblHeading.setText("An exception occured with the following message:");
        jPanel5.add(lblHeading, java.awt.BorderLayout.NORTH);

        lblPicture.setIcon(MediaCache.getErrorIcon());
        jPanel5.add(lblPicture, java.awt.BorderLayout.WEST);

        jPanel1.setLayout(new java.awt.BorderLayout(0, 5));

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 0, 0));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(),
                javax.swing.BorderFactory.createEmptyBorder(10, 15, 15, 10)));
        scrollMessage.setBorder(null);
        txtMessage.setOpaque(true);
        scrollMessage.setViewportView(txtMessage);

        jPanel2.add(scrollMessage, java.awt.BorderLayout.CENTER);

        jPanel4.add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        btnClose.setText("Ok");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JDialogError.this.dispose();
            }
        });

        jPanel3.add(btnClose);

        jPanel1.add(jPanel3, java.awt.BorderLayout.SOUTH);
        scrollMessage.setBackground(Color.green);

        jPanel5.add(jPanel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
    }

    private javax.swing.JButton btnClose;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel lblHeading;
    private javax.swing.JLabel lblPicture;
    private javax.swing.JScrollPane scrollMessage;
    private JLabel txtMessage;

}
